const { sequelize } = require('../config/db');
const { DataTypes } = require('sequelize');

// 定义用户模型
const User = sequelize.define('user', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  username: {
    type: DataTypes.STRING(50),
    allowNull: false,
    comment: '用户名'
  },
  password: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: '密码'
  },
  phone: {
    type: DataTypes.STRING(20),
    allowNull: false,
    unique: false,
    validate: {
      is: /^1[3-9]\d{9}$/
    },
    comment: '手机号'
  },
  gender: {
    type: DataTypes.INTEGER,
    allowNull: true,
    defaultValue: 0,
    comment: '性别：0-未知，1-男，2-女'
  },
  birthDate: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '出生日期'
  },
  status: {
    type: DataTypes.INTEGER,
    defaultValue: 1,
    comment: '状态：0-禁用，1-启用'
  }
}, {
  tableName: 'users',
  comment: '用户表',
  paranoid: true, // 启用软删除
  deletedAt: 'deletedAt'
});

// 可以在这里定义其他模型...

// 定义患者模型
const Patient = sequelize.define('patient', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  name: {
    type: DataTypes.STRING(50),
    allowNull: false,
    comment: '姓名'
  },
  gender: {
    type: DataTypes.INTEGER,
    allowNull: true,
    defaultValue: 0,
    comment: '性别：0-未知，1-男，2-女'
  },
  birthDate: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '出生日期'
  },
  memberRelationship: {
    type: DataTypes.STRING(20),
    allowNull: true,
    comment: '成员关系'
  },
  idNumber: {
    type: DataTypes.STRING(18),
    allowNull: true,
    unique: true,
    validate: {
      is: /(^\d{18}$)|(^\d{17}(\d|X|x)$)/ // 身份证号验证正则
    },
    comment: '身份证号'
  },
  phone: {
    type: DataTypes.STRING(11),
    allowNull: true,
    unique: true,
    validate: {
      is: /^1[3-9]\d{9}$/ // 手机号验证正则
    },
    comment: '手机号'
  }
}, {
  tableName: 'patients',
  comment: '患者列表',
  paranoid: true, // 启用软删除
  deletedAt: 'deletedAt'
});
// 导出所有模型
module.exports = {
  User,
  Patient,
  sequelize // 如果需要也可以导出sequelize实例
};